package com.jq.invoicing.test.dao.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jq.invoicing.test.dao.ISysuserDAO;
import com.jq.invoicing.test.vo.Person;

public class SysuserDAOHibernate extends HibernateDaoSupport implements
		ISysuserDAO {

	@SuppressWarnings("unchecked")
	public Person getPerson(String id) {
		// TODO Auto-generated method stub
		List<Person> ps = getHibernateTemplate().find("from Person where id=?",id);
		if(ps.size()>0){
			return ps.get(0);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public Person getPersonByAccount(String account) {
		// TODO Auto-generated method stub
		List<Person> ps = getHibernateTemplate().find("from Person where account=?",account);
		if(ps.size()>0){
			return ps.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Person> getPersons() {
		// TODO Auto-generated method stub
		List<Person> ps = getHibernateTemplate().find("from Person");
		return ps;
	}

	public void removesPerson(Person p) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(p);
	}

	public void savePerson(Person p) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(p);
	}

	private String buildSql(Person p) throws Exception{
		StringBuffer sql = new StringBuffer("from Person where 1=1 ");
		if(p!=null){
			if(StringUtils.isNotEmpty(p.getId())){
				sql.append(" and id = '" + p.getId() + "'");
			}
			if(StringUtils.isNotEmpty(p.getName())){
				sql.append(" and name like '%" + p.getName() + "%'");
			}
			if(StringUtils.isNotEmpty(p.getAccount())){
				sql.append(" and account like '%" + p.getAccount() + "%'");
			}
			if(StringUtils.isNotEmpty(p.getAddress())){
				sql.append(" and address like '%" + p.getAddress() + "%'");
			}
			if(StringUtils.isNotEmpty(p.getPwd())){
				sql.append(" and pwd like '%" + p.getPwd() + "%'");
			}	
			/*if(userInfo.getOptions() != null && userInfo.getOptions().intValue() > -1){
				sql = sql + " and options = " + userInfo.getOptions() + "";
			}*/
		}
		return sql.toString();
	}
	
	public int selectCount(Person p) throws Exception{
		String sql = buildSql(p);
		sql = "select count(*) " + sql;
		Session session = this.getSession();
		Query query = session.createQuery(sql);
		return ((Long) query.iterate().next()).intValue();
	}
	
	@SuppressWarnings("unchecked")
	public List<Person> selectAll() throws Exception {
		return this.getHibernateTemplate().find("from Person");
	}
	
	@SuppressWarnings("unchecked")
	public List<Person> select(Person p,int pageNum,int pageSize) throws Exception {
		String sql = buildSql(p);
		sql = sql + " order by id";
		Session session = this.getSession();
		Query query = session.createQuery(sql);
		query.setFirstResult((pageNum-1) * pageSize);
		query.setMaxResults(pageSize);
		return query.list();
	}
	
}
